import 'package:flutter/material.dart';

class FocusTest extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return _FocusTestState();
   
  }
  
}


class _FocusTestState extends State<FocusTest>{
  FocusNode focusNode1 = FocusNode();
  FocusNode focusNode2 = FocusNode();
  FocusScopeNode? focusScopeNode;

  @override
  void initState() {
    super.initState();
    focusNode1.addListener((){
      print("focusNode1: ${focusNode1.hasFocus}");
    });

    focusNode2.addListener((){
      print("focusNode1: ${focusNode2.hasFocus}");
    });
  }




  @override
  Widget build(BuildContext context) {
    return Theme(
      data: Theme.of(context).copyWith(
        hintColor: Colors.grey[200],
        inputDecorationTheme: InputDecorationTheme(
          labelStyle: TextStyle(color: Colors.red),
          hintStyle: TextStyle(color: Colors.grey,fontSize: 14.0)
        )
      ),
      child: Padding(
      padding: EdgeInsets.all(16.0),
      child: Column(
        children: [
          Container(
            child: TextField(
              autofocus: true,
              focusNode: focusNode1,
              decoration: InputDecoration(
              labelText: "input1"
              ),
            ),
            decoration: BoxDecoration(
              border: Border(bottom: BorderSide(color: Colors.green, width: 1.0))
            ),
          )
          ,
          TextField(
            focusNode: focusNode2,
            decoration: InputDecoration(
              labelText: "input2",
              enabledBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: Colors.green)
              ),
              focusedBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: Colors.blue)
              )
            ),
          ),
          Builder(builder: (ctx) {
            return Column(
              children: [
                ElevatedButton(
                  onPressed: (){
                    focusScopeNode ??= FocusScope.of(context);
                    focusScopeNode?.requestFocus(focusNode2);

                  }, 
                  child: Text("移动焦点")
                ),
                ElevatedButton(
                  onPressed: (){
                    focusNode1.unfocus();
                    focusNode2.unfocus();

                  }, 
                  child: Text("隐藏键盘"),
                )
              ],

            );
          })
        ]
      ),
      )
    );
  }
  
}